package com.kfh.ybracelet.location;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.bean.LocationBean;
import com.kfh.ybracelet.my.ModifyPhoneActivity;

/**
 * 手环状态页面
 * Created by Aaron on 15/8/10.
 */
public class WatchStatusActivity extends BaseActivity {

    private TextView tv_nickname;
    private TextView tv_tel;
    private TextView tv_electric_quantity;
    private TextView tv_sign;
    private TextView tv_address;
    //    private TextView tv_invite_code;
    //    private ListView lv_binder;
//    private List<CarePeople> list=new ArrayList<CarePeople>();
//    private UserAdapter userAdapter;
    private LocationBean.LocationChildInfo child;
    //    private ImageView iv_sos;
    //通过上一个activity把数据传到本activity
    private static final int MODIFY_MY_PHONE_CODE = 11002;
    private TextView tv_sn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_watch_status);
//        mApplication.addActivity(this);
        child = (LocationBean.LocationChildInfo) getIntent().getSerializableExtra("child");

        initWidget();

        setData();

    }

    private void setData() {

        if (child == null)
            return;

        if (child.device == null)
            return;

        tv_nickname.setText(child.name + (child.device.online ? "(在线)" : "(离线)"));
        tv_tel.setText(child.device.phoneNum);
        tv_electric_quantity.setText(child.device.battery + "%");
        tv_sign.setText(child.device.gsm + "%");
        tv_address.setText(child.device.location);
//        tv_invite_code.setText(child.inviteKey);
//        //是否双向通话
//        iv_sos.setSelected(child.device.sosPhone);
        tv_sn.setText(child.device.sn);

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.watch_state));

        tv_nickname = (TextView) findViewById(R.id.tv_nickname);
        tv_tel = (TextView) findViewById(R.id.tv_tel);
        tv_electric_quantity = (TextView) findViewById(R.id.tv_electric_quantity);
        tv_sign = (TextView) findViewById(R.id.tv_sign);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_sn = (TextView) findViewById(R.id.tv_sn);
//        tv_invite_code = (TextView) findViewById(R.id.tv_invite_code);

        RelativeLayout rl_tel = (RelativeLayout) findViewById(R.id.rl_tel);

//        iv_sos = (ImageView) findViewById(R.id.iv_sos);

        iv_left.setOnClickListener(this);
        rl_tel.setOnClickListener(this);
//        iv_sos.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;
            case R.id.rl_tel:

                //更改电话?
                Intent mIntent = new Intent(WatchStatusActivity.this, ModifyPhoneActivity.class);
                if (child != null)
                    mIntent.putExtra("id", child.id);
                startActivityForResult(mIntent, MODIFY_MY_PHONE_CODE);

                break;

//            case R.id.iv_sos://双向通话
//
//                setSosReq();
//
//                break;

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data == null)
            return;

        if (resultCode == MODIFY_MY_PHONE_CODE) {

            tv_tel.setText(data.getStringExtra("phone"));

        }

    }

//    /**
//     * 设置双向通话
//     */
//    private void setSosReq() {
//
//        JSONObject j = new JSONObject();
//
//        j.put("token", sp.getString(Constant.TOKEN, ""));
//        //0 不开通双向通话 , 1 开通双向通话
//        if (child != null && child.device != null)
//            j.put("sn", child.device.sn);
//        j.put("flag", iv_sos.isSelected() ? 0 : 1);
//
//        HttpUtil.setPostRequest("设置中...", this, Constant.WEB_ROOT + Constant.SOSTELL,
//                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
//                    @Override
//                    public void setOnSuccessListener(String str) {
//
//                        try {
//
//                            org.json.JSONObject result = new org.json.JSONObject(str);
//
//                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {
//
//                                showToast("设置成功");
//
//                                iv_sos.setSelected(!iv_sos.isSelected());
//
//                            } else {
//                                showToast(Constant.showErrorMsg(result.getInt("code"), WatchStatusActivity.this));
//                            }
//
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            showToast("设置失败");
//                        }
//
//                    }
//                }, null);
//
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

}